﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Xml.Serialization;

namespace WeatherPrediction
{
    static class Communication
    {

        static public string GetResponseOfRequestCountryCode(ParseResult locationsResponse)
        {
            string countryCode = locationsResponse.results[0].countryCode;
            return countryCode;
        }

        static public string GetResponseOfRequestCountryName(ParseResult locationsResponse)
        {
            string countryName = locationsResponse.results[0].countryName;
            countryName = countryName.Substring(GetIndexCountryName(countryName));
            return countryName;
        }

        private static int GetIndexCountryName(string countryName)
        {
            for (int i = 0; i < countryName.Length; i++) 
            {
                if (countryName[i] == ',') 
                {
                    // plus two because there is a space between city name and country name
                    return i+2;
                }
            }
            return 0;
        }

        static public ParseForecast GetResponseOfRequestForecast(ParseForecast weatherForecastsResponse)
        {
            return weatherForecastsResponse;            
        }

        static public ParseResult MakeRequestCountryCode(string requestUrl,string responseFormat)
        {
            WebClient web = new WebClient();
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response != null && response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));
                    }

                    if (responseFormat == ".json")
                    {
                        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof (ParseResult));
                        object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                        ParseResult jsonResponse = objResponse as ParseResult;
                        return jsonResponse;
                    }
                    else
                    {
                        //TO DO
                        /*XmlSerializer xmlSerializer = new XmlSerializer(typeof(ParseResult));
                        object objResponse = xmlSerializer.ReadObject(response.GetResponseStream());
                        ParseResult jsonResponse = objResponse as ParseResult;
                        return jsonResponse;*/
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }

            return null;
        }

        static public ParseForecast MakeRequestForecast(string requestUrl)
        {
            WebClient web = new WebClient();

            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));
                    }
                    DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ParseForecast));
                    object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                    ParseForecast jsonResponse = objResponse as ParseForecast;
                    return jsonResponse;
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        
    }
}
